package com.cmwa.solution.business.investTrade.controller;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Employee;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.investTrade.entity.InvesSecurVo;
import com.cmwa.solution.business.investTrade.service.InvesSecurService;
import com.cmwa.solution.utils.SessionUtils;

import net.sf.json.JSONObject;

/**
 * 投资标的控制器
 * @author ex-chenbq
 *
 */
@Controller
@RequestMapping("/business/invesSecur")
public class InvesSecurController extends BaseController {

	private final static String JSP_PATH = "business/investTrade/";
	@Resource
	private InvesSecurService invesSecurService;
		
	/**
	 * 跳转投资标的页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/investTradeListView")
	public String investTradeListView(@Param("prdCode") String prdCode,@Param("securType") String securType,ModelMap model) {
		model.addAttribute("prdCode", prdCode);
		model.addAttribute("securType", securType);
		return JSP_PATH + "investTradeList";
	}
	
	/**
	 * 跳转投资标的新增页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/investTradeAddView")
	public ModelAndView investTradeAddView() {
		ModelAndView model = new ModelAndView();
		model.addObject("method","add");
		model.setViewName(JSP_PATH + "investTradeAdd");
		return model;
	}
	
	/**
	 * 跳转投资标的修改页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/investTradeUpdateView")
	public ModelAndView investTradeUpdateView(@RequestParam("id") String id) {
		ModelAndView model = new ModelAndView();
		InvesSecurVo invesSecurVo = invesSecurService.findInvesSecurById(id);
		if (null == invesSecurVo) {
			model.addObject("exception", "投资标的信息不存在");
			model.setViewName("system/hint/validateFailed");
			return model;
		}
		model.addObject("vo", invesSecurVo);
		model.addObject("method","update");
		model.setViewName(JSP_PATH + "investTradeAdd");
		return model;
	}
	

	/**
	 * 查看投资标的详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/investTradeDetailView")
	public ModelAndView investTradeDetailView(@RequestParam("id") String id) {
		ModelAndView model = new ModelAndView();
		InvesSecurVo invesSecurVo = invesSecurService.findInvesSecurById(id);
		if (null == invesSecurVo) {
			model.addObject("exception", "投资标的信息不存在");
			model.setViewName("system/hint/validateFailed");
			return model;
		}
		model.addObject("vo", invesSecurVo);
		model.addObject("method","detail");
		model.setViewName(JSP_PATH + "investTradeAdd");
		return model;
	}

	/**
	 * 投资标的数据分页查询
	 * @param param
	 * @return
	 */
	@RequestMapping("/investTradeListPage")
	@ResponseBody
	public Page<InvesSecurVo> investTradeListPage(SearchParam param) {
		Page<InvesSecurVo> list = invesSecurService.queryInvesSecurListPage(param);
		return list;
	}
	
	/**
	 * 新增投资标的
	 * @param invesSecurVo
	 * @return
	 */
	@RequestMapping("/insertInvesSecur")
	public ModelAndView insertInvesSecur(InvesSecurVo invesSecurVo) {
		ModelAndView model = new ModelAndView();
		Employee employee = SessionUtils.getEmployee();
		try {
			invesSecurVo.setCreateId(employee.getID());
			invesSecurVo.setModifyId(employee.getID());
			invesSecurService.insertInvesSecur(invesSecurVo);
			model.setViewName("system/hint/success");
		} catch (Exception e) {
			e.printStackTrace();
			model.addObject("exception", e.getMessage());
			model.setViewName("system/hint/validateFailed");
		}
		return model;
	}
	

	/**
	 * 修改投资标的
	 * @param invesSecurVo
	 * @return
	 */
	@RequestMapping("/updateInvesSecur")
	public ModelAndView updateInvesSecur(InvesSecurVo invesSecurVo) {
		ModelAndView model = new ModelAndView();
		Employee employee = SessionUtils.getEmployee();
		try {
			invesSecurVo.setModifyId(employee.getID());
			invesSecurService.updateInvesSecur(invesSecurVo);
			model.setViewName("system/hint/success");
		} catch (Exception e) {
			e.printStackTrace();
			model.addObject("exception", e.getMessage());
			model.setViewName("system/hint/validateFailed");
		}
		return model;
	}
	
	/**
	 * 删除投资标的
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteInvesSecur")
	@ResponseBody
	public JSONObject deleteInvesSecur(@RequestParam("id") String id) {
		JSONObject jsonObject = new JSONObject();
		try {
			invesSecurService.deleteInvesSecurById(id);
			jsonObject.put("resultCode", "0000");
			jsonObject.put("resultMsg", "投资标的信息删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put("resultCode", "9999");
			jsonObject.put("resultMsg", "投资标的信息删除失败："+e.getMessage());
		}
		return jsonObject;
	}
}

